package com.cmft.generator.db.service;

import com.cmft.generator.db.entity.TableField;
import com.cmft.generator.db.entity.TableInfo;
import com.cmft.generator.db.tool.DBTool;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MysqlTableServiceImpl implements TableService {

    @Override
    public String getTableFieldSql(TableInfo tableInfo) {
        return String.format("SELECT column_name,data_type,column_comment FROM information_schema.COLUMNS WHERE table_schema='%s' AND table_NAME = '%s'",
                tableInfo.getTableSchema(), tableInfo.getTableName());
    }

    @Override
    public TableField getTableField(ResultSet rs) throws SQLException {
        TableField tableField = new TableField();
        tableField.setColumn(rs.getString("column_name"));
        tableField.setJdbcType(rs.getString("data_type"));
        tableField.setComment(rs.getString("column_comment"));

        tableField.setProperty(DBTool.tuoFeng(tableField.getColumn()));
        tableField.setJavaType(DBTool.columnTypeToBeanType(tableField.getJdbcType()));
        return tableField;
    }
}